import O1HD, { managers } from "../../../../engines/gameEngine";
import data_control from "../../../../engines/libs/data_control";
import { INormalLevelStruct } from "../../../../Game/Interface";
import tipsPageSpNode from "../../../../Game/Obj/tipsPageSpNode";
import levelAvoidEnemy, { EnemyMoveType } from "../base/levelAvoidEnemy";
import levelAvoidPlayer from "../base/levelAvoidPlayer";
import levelAvoidBase from "../levelAvoidBase";

const { ccclass, property } = cc._decorator;

@ccclass
export default class level29 extends levelAvoidBase {
    async Init(info: INormalLevelStruct): Promise<void> {
        await super.Init(info)
        O1HD.getManagers().soundMgr.loadSoundBundle("fontChase");
        // this.node_ma.node.opacity = 0;
        // this.node_ma.init("马", this.player.getComponent(levelAvoidPlayer), {
        //     moveType: 2,
        //     moveSpeed: 10,
        //     startPos: this.node_ma.node.getPosition(),
        //     attRanglePos: cc.v2(300, 100)
        // }, this)
        // this.node_ma.startAvoid()
    }

    setGameStart() {
        super.setGameStart();

        // managers().soundMgr.playSound("level29/sound/startGame", () => { })
        this.wave1();
        // this.wave3();

    }
    waveNode: cc.Node[] = [];
    wave1() {

        let zuoUpPos: cc.Vec2[] = [
            cc.v2(-1, 0).rotateSelf(40 * Math.PI / 180),
            cc.v2(-1, 0).rotateSelf(55 * Math.PI / 180),
            cc.v2(-1, 0).rotateSelf(70 * Math.PI / 180),
            cc.v2(-1, 0).rotateSelf(85 * Math.PI / 180),
            cc.v2(-1, 0).rotateSelf(100 * Math.PI / 180),
            cc.v2(-1, 0).rotateSelf(115 * Math.PI / 180),
        ]
        let youUpPos: cc.Vec2[] = [
            cc.v2(-1, 0).rotateSelf(90 * Math.PI / 180),
            cc.v2(-1, 0).rotateSelf(100 * Math.PI / 180),
            cc.v2(-1, 0).rotateSelf(110 * Math.PI / 180),
            cc.v2(-1, 0).rotateSelf(120 * Math.PI / 180),
            cc.v2(-1, 0).rotateSelf(130 * Math.PI / 180),
            cc.v2(-1, 0).rotateSelf(140 * Math.PI / 180),
        ]

        let zuoDownPos: cc.Vec2[] = [
            cc.v2(1, 0).rotateSelf(90 * Math.PI / 180),
            cc.v2(1, 0).rotateSelf(100 * Math.PI / 180),
            cc.v2(1, 0).rotateSelf(110 * Math.PI / 180),
            cc.v2(1, 0).rotateSelf(120 * Math.PI / 180),
            cc.v2(1, 0).rotateSelf(130 * Math.PI / 180),
            cc.v2(1, 0).rotateSelf(140 * Math.PI / 180),
        ]

        let youDownPos: cc.Vec2[] = [
            cc.v2(1, 0).rotateSelf(60 * Math.PI / 180),
            cc.v2(1, 0).rotateSelf(70 * Math.PI / 180),
            cc.v2(1, 0).rotateSelf(80 * Math.PI / 180),
            cc.v2(1, 0).rotateSelf(90 * Math.PI / 180),
            cc.v2(1, 0).rotateSelf(100 * Math.PI / 180),
            cc.v2(1, 0).rotateSelf(110 * Math.PI / 180),
        ]

        let chats:string[] = ["咀","嚼","吞","咽","啖","肉","食","吃","喝","饕","餮"];
        this.scheduleOn(() => {
            for (let j = 0; j <3; j++) {
                for (let i = 0; i < 6; i++) {
                    let chat:string = data_control.getRandItemOfArray(chats);
                    this.scheduleOn(() => {
                        let LabNode = this.newLabNode();
                        let pos = j % 2 == 0 ? zuoUpPos[i] : youUpPos[i]
                        LabNode.init(chat, this.player.getComponent(levelAvoidPlayer), {
                            moveType: 1,
                            moveSpeed: 5,
                            startPos: cc.v2(0,355),  //this.node_ma.node.getPosition(),
                            moveDirection: pos
                        }, this)
                        LabNode.startAvoid()
                        this.waveNode.push(LabNode.node)
                        this.scheduleOn(() => {
                            managers().soundMgr.playSound("fontChase/shot", () => {
                            })
                        }, 0.5);
                    }, j * 0.5)
                }

            }
        }, 1);

        this.scheduleOn(() => {
            for (let j = 0; j < 3; j++) {
                for (let i = 0; i < 6; i++) {
                    let chat:string = data_control.getRandItemOfArray(chats);
                    this.scheduleOn(() => {
                        let LabNode = this.newLabNode();
                        let pos = j % 2 == 0 ? youDownPos[i] : zuoDownPos[i]
                        LabNode.init(chat, this.player.getComponent(levelAvoidPlayer), {
                            moveType: 1,
                            moveSpeed: 5,
                            startPos: cc.v2(0,-355),
                            moveDirection: pos
                        }, this)
                        LabNode.startAvoid()
                        this.waveNode.push(LabNode.node)
                        this.scheduleOn(() => {

                            managers().soundMgr.playSound("fontChase/shot", () => {
                            })
                        }, 0.3)
                    }, j * 0.5)
                }
            }

        }, 4)
        this.scheduleOn(() => {
            for (let node of this.waveNode) node.destroy()
            this.waveNode = []
            this.wave4()
        }, 8)
    }
    wave2() {

        // managers().soundMgr.playSound("level29/sound/mztc")
        // cc.tween(this.node_ma.node)
        //     .to(0.2, { scale: 0 })
        //     .delay(1)
        //     .call(() => {
        //         this.node_ma.lab.string = "人"
        //         this.node_ma.node.getChildByName("maren").active = true;

        //         managers().soundMgr.playSound("level29/sound/maren")
        //         this.scheduleOn(() => {
        //             this.node_ma.node.getChildByName("maren").active = false;
        //         }, 4)
        //     })
        //     .to(0.2, { scale: 1 })
        //     .start()

        let strList: string[] = [
            "你\n好\n，\n小\n肥\n猫",
            "心\n广\n体\n肥",
            "体\n重\n焦\n虑",
            "胖\n得\n相\n亲\n对\n象\n连\n夜\n跑\n路\n",
            "你\n这\n么\n胖\n，\n根\n本\n买\n不\n到\n衣\n服",
            "你\n胖\n成\n这\n样\n，\n还\n能\n嫁\n的\n出\n去",
            "你\n走\n过\n的\n时\n候\n，\n我\n感\n觉\n地\n在\n颤\n抖",
            "你\n不\n胖\n，\n只\n是\n照\n片\n的\n空\n间\n小\n了\n点",
            "每\n天\n都\n在\n横\n向\n发\n展",
            "电\n梯\n超\n重\n，\n大\n家\n全\n都\n看\n着\n你",
            "一\n白\n遮\n百\n丑\n，\n一\n胖\n毁\n所\n有\n",
            "只\n是\n骨\n架\n大\n，\n才\n不\n是\n胖\n",
            "不\n好\n意\n思\n，\n这\n里\n没\n有\n你\n的\n尺\n码",
            "站\n远\n点\n，\n你\n挡\n我\n信\n号\n了",
            "燕\n瘦\n环\n肥\n，\n你\n是\n最\n后\n一\n项"
        ]
        let posYList = [-250, -200, -150, -75, 0, 75, 150, 200, 250]
        let yList = posYList.concat()
        let tempStrList = strList.concat();
        strList.sort((a, b) => { return Math.random() < 0.5 ? 1 : -1 })
        for (let i = 0; i < 30; i++) {
            this.scheduleOn(() => {
                // cc.tween(this.node)
                //     .delay(i * 0.5)
                // .call(() => {
                let x = Math.random() < 0.5 ? -1000 : 1000;

                if (yList.length == 0) yList = posYList.concat()
                let indexof = Math.floor(data_control.getRandNumberOfArrayTwo([0, yList.length]))
                let startPos = cc.v2(yList[indexof],x);
                yList.splice(indexof, 1)

                let moveDirection = cc.v2(0,x > 0 ? -1 : 1)
                let LabNode = this.newLabNode();

                if (tempStrList.length == 0) tempStrList = strList.concat();
                let strIndex = Math.floor(data_control.getRandNumberOfArrayTwo([0, tempStrList.length]))
                let str = tempStrList[strIndex];
                tempStrList.splice(strIndex, 0)

                LabNode.init(str, this.player.getComponent(levelAvoidPlayer), {
                    moveType: 1,
                    moveSpeed: 5,
                    startPos: startPos,
                    moveDirection: moveDirection
                }, this)
                LabNode.startAvoid()
                this.waveNode.push(LabNode.node)
                // })
                // .start()
            }, i * 0.5)
        }


        this.scheduleOn(() => {

            for (let node of this.waveNode) node.destroy()
            this.waveNode = []
            this.wave3()
        }, 20)
    }

    wave3() {
        // this.node_ma.startAvoid(false);
        let labList: string[] = ["肥", "胖", "丑", "油", "腻"]
        let posList: cc.Vec2[] = [cc.v2(-280, 280), cc.v2(-280, 0), cc.v2(-280, -280), cc.v2(280, 280), cc.v2(280, 0), cc.v2(280, -280)]
        for (let i = 0; i < labList.length; i++) {

            let LabNode = this.newLabNode();
            this.waveNode.push(LabNode.node)
            LabNode.init(labList[i], this.player.getComponent(levelAvoidPlayer), {
                moveType: 0,
                moveSpeed: 1.5,
                startPos: posList[i]//cc.v2(data_control.getRandNumberOfArrayTwo([-280, 280]), data_control.getRandNumberOfArrayTwo([-380, 380]))
            }, this,1.25)
            LabNode.startAvoid()
        }
        this.scheduleAny(() => {
            let lab = labList[Math.floor(data_control.getRandNumberOfArrayTwo([0, labList.length]))]
            let LabNode = this.newLabNode();
            this.waveNode.push(LabNode.node)
            LabNode.node.scale = 1.5
            LabNode.init(lab, this.player.getComponent(levelAvoidPlayer), {
                moveType: 0,
                moveSpeed: 1.5,
                startPos: cc.v2(data_control.getRandNumberOfArrayTwo([-280, 280]), data_control.getRandNumberOfArrayTwo([-380, 380]))
            }, this,1.25)
            LabNode.startAvoid()
        }, 0.5)
    }

    wave4(){
        let posAry:cc.Vec2[] = [
            cc.v2(-255,355),
            cc.v2(255,355),
            cc.v2(-255,-355),
            cc.v2(255,-355),
        ];

        let posAryCopy:cc.Vec2[] = data_control.randomSortArray(posAry.concat());

        let chats:string[] = ["披萨","熏火腿","五香豆","担担面","叉烧包","虾饺","章鱼小丸子","紫菜","奥尔良烤翅","狗不理包子","黑森林","布朗尼","提拉米苏","牛排","牛肉干","奶昔","奶茶","双皮奶","手抓饼","巧克力","咖喱鱼丸","鱼腐","寿司","北极贝","三文鱼","冰激凌","炒面","三文治","鸡翅","意大利面","烧烤","鳗鱼饭三吃","拉面","熟寿司","荞麦面"]

        let createCount:number = 10;
        let interval:number = 1;
        let chatCopy = data_control.randomSortArray(chats.concat());
        for(let i = 0;i<createCount;i++){
            this.scheduleOn(()=>{
                if(chatCopy.length <=0){
                    chatCopy = data_control.randomSortArray(chats.concat());
                }
                let chat:string = chatCopy.pop();
                let LabNode = this.newLabNode();
                if(posAryCopy.length <= 0){
                    posAryCopy = data_control.randomSortArray(posAry.concat());
                }
                let pos = posAryCopy.pop();
                let dir = this.player.getPosition().sub(pos).normalize();
                LabNode.init(chat, this.player.getComponent(levelAvoidPlayer), {
                    moveType: 1,
                    moveSpeed: 6,
                    startPos: pos,
                    moveDirection: dir
                }, this)
                LabNode.startAvoid()
                this.waveNode.push(LabNode.node);
            },i * interval);
        }

        this.scheduleOn(()=>{
            this.wave2();
        },createCount * interval + 3);
    }


    Lose(time?: number) {
        this.unscheduleAllCallbacks()
        let pos = this.player
        let jumpTime = cc.v3(100, -1000).sub(this.player.position).mag() / 800
        // managers().soundMgr.playSound("fontChase/death").then();
        cc.tween(this.player)
            .parallel(
                cc.tween(this.player).bezierTo(jumpTime, this.player.getPosition(), cc.v2(this.player.x + 200, this.player.y + 500), cc.v2(this.player.x + 400, this.player.y - 1000)),
                cc.tween(this.player).to(4, { angle: -3000 })
            )
            .start()
        super.Lose(jumpTime)

    }
    Win(time?: number, jyIndex?: number, userStr?: string): void {
        this.unscheduleAllCallbacks()
        let endScale = this.player.scale
        managers().soundMgr.playSound("fontChase/win", () => { })
        cc.tween(this.player)
            .to(0.2, { scale: 0.6 })
            .call(() => {
                this.player.getChildByName("spwin").active = true;
            })
            .delay(2)
            .call(() => {
                let bo = this.player.getChildByName("bo")
                bo.active = true;

                cc.tween(bo)
                    .to(1, { scale: 5 })
                    .call(() => {
                        bo.active = false;
                    })
                    .start()
            })
            .to(0.2, { scale: 1 })
            .call(() => {
                for (let node of this.waveNode) {
                    cc.tween(node)
                        .to(0.1, { scale: 1.1, opacity: 255 })
                        .to(0.1, { scale: 1, opacity: 100 })
                        .to(0.1, { scale: 1.1, opacity: 255 })
                        .to(0.1, { scale: 1, opacity: 100 })
                        .to(0.3, { scaleX: 1.2, scaleY: 0.8, opacity: 0 })
                        .call(() => {
                            node.destroy()
                        })
                        .start()
                }
                super.Win(3)
            })
            .start()

    }
}